/*
 * Copyright 2019 Flipkart Internet Pvt. Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flipkart.ohos.proteus.value;

import com.flipkart.ohos.proteus.toolbox.Utils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Layout
 *
 * @author aditya.sharat
 */
public class Layout extends Value {
    public final String type;

    public final List<Attribute> attributes;

    public final Map<String, Value> data;

    public final ObjectValue extras;

    public Layout(String type, List<Attribute> attributes, Map<String, Value> data, ObjectValue extras) {
        this.type = type;
        this.attributes = attributes;
        this.data = data;
        this.extras = extras;
    }

    @Override
    public Layout copy() {
        List<Attribute> attributeList = null;
        if (this.attributes != null) {
            attributeList = new ArrayList<>(this.attributes.size());
            for (Attribute attribute : this.attributes) {
                attributeList.add(attribute.copy());
            }
        }
        return new Layout(type, attributeList, data, extras);
    }

    public Layout merge(Layout include) {
        List<Attribute> attributes = null;
        if (this.attributes != null) {
            attributes = new ArrayList<>(this.attributes.size());
            attributes.addAll(this.attributes);
        }
        if (include.attributes != null) {
            if (attributes == null) {
                attributes = new ArrayList<>(include.attributes.size());
            }
            attributes.addAll(include.attributes);
        }

        Map<String, Value> dataMap = null;
        if (this.data != null) {
            dataMap = this.data;
        }
        if (include.data != null) {
            if (dataMap == null) {
                dataMap = new LinkedHashMap<>(include.data.size());
            }
            dataMap.putAll(include.data);
        }

        ObjectValue objectExtras = new ObjectValue();
        if (this.extras != null) {
            objectExtras = Utils.addAllEntries(objectExtras, this.extras);
        }
        if (include.extras != null) {
            if (objectExtras == null) {
                objectExtras = new ObjectValue();
            }
            Utils.addAllEntries(objectExtras, include.extras);
        }
        return new Layout(type, attributes, dataMap, objectExtras);
    }

    /**
     * Attribute
     *
     * @author aditya.sharat
     */
    public static class Attribute {
        public final int id;
        public final Value value;

        public Attribute(int id, Value value) {
            this.id = id;
            this.value = value;
        }

        protected Attribute copy() {
            return new Attribute(id, value.copy());
        }
    }
}
